﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hi_Baidu.Packet.InPacket
{
    /// <summary>
    ///     回馈用户心跳请求包
    /// </summary>
    class UserHeartBreathReplyPacket : BaseInPackage
    {
        public UserHeartBreathReplyPacket(InPackage inpackage) : base(inpackage)
        {
        }

        public override void InitPackage()
        {
            int Offset = 0;
            //用户名长度
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //用户名
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            //会话ID长度
            byte[] SessionLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, SessionLength, 0, 4);
            Offset += 4;
            //会话
            byte[] Session = new byte[BitConverter.ToInt32(SessionLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Session, 0, Session.Length);
            Offset += Session.Length;
            byte[] Result = new byte[1];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Result, 0, 1);
            Offset += 1;

            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("SESSION", Session);
            this.DIC_CONTENT.Add("HEARTBREATH", Result);
        }

        /// <summary>
        ///     得到心跳状态
        /// </summary>
        /// <returns>返回心跳状态</returns>
        public bool GetHeartBreathState()
        {
            return BitConverter.ToBoolean(this.DIC_CONTENT["HEARTBREATH"], 0);
        }
    }
}
